#include <nice2d/threadtask.h>

class ThreadTaskData
{
public:
    template<class C>
    ThreadTaskData(C* task):
        thread(&C::run,task)
    {
        started = false;
        done = true;
    }

    sf::Thread thread;
    bool started;
    bool done;
};

ThreadTask::ThreadTask():
    data(new ThreadTaskData(this))
{
}

ThreadTask::~ThreadTask()
{}

void ThreadTask::start()
{
    if ((data->done == true) && (data->started == false))
	{
        data->started = true;
        data->done = false;
        data->thread.launch();
	}
}

bool ThreadTask::isStarted() const
{
    return data->started;
}

bool ThreadTask::isDone() const
{
    return data->done;
}

void ThreadTask::wait(int mstime) const
{
    sf::Time duration(sf::milliseconds(mstime));
    while (isDone() == false)
	{
        sf::sleep(duration);
    }
}

void ThreadTask::makeDone()
{
    data->done = true;
}
